package com.charger.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.charger.chargingStation.domain.Chargingstation;
import com.charger.chargingStation.service.IChargingstationService;
import com.charger.domain.Dispose;
import com.charger.service.IDisposeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.domain.FailureManage;
import com.charger.service.IFailureManageService;
import com.charger.common.utils.poi.ExcelUtil;
import com.charger.common.core.page.TableDataInfo;

/**
 * 故障处理Controller
 *
 * @author xuan
 * @date 2024-11-27
 */
@RestController
@RequestMapping("/risksFailure/failureDispose")
public class FailureManageController extends BaseController {
    @Autowired
    private IFailureManageService failureManageService;
    @Autowired
    private IDisposeService disposeService;
    @Autowired
    private IChargingstationService chargingstationService;

    /**
     * 查询故障处理列表
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:failureDispose:list')")
    @GetMapping("/list")
    public TableDataInfo list(FailureManage failureManage) {
        startPage();
        List<FailureManage> list = failureManageService.selectFailureManageList(failureManage);
        list.forEach(fai -> {
            // 修改处理表
            if (fai.getDisposeId() != null)
                fai.setDispose(disposeService.selectDisposeById(fai.getDisposeId()));
            // 修改充电站
            if (fai.getChargerStationId() != null)
                fai.setChargingstation(chargingstationService.selectChargingstationByChargingstationId(fai.getChargerStationId()));
        });
        return getDataTable(list);
    }

    /**
     * 导出故障处理列表
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:failureDispose:export')")
    @Log(title = "故障处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FailureManage failureManage) {
        List<FailureManage> list = failureManageService.selectFailureManageList(failureManage);
        ExcelUtil<FailureManage> util = new ExcelUtil<FailureManage>(FailureManage.class);
        util.exportExcel(response, list, "故障处理数据");
    }

    /**
     * 获取故障处理详细信息
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:failureDispose:query')")
    @GetMapping(value = "/{failureId}")
    public AjaxResult getInfo(@PathVariable("failureId") Long failureId) {
        FailureManage failureManage = failureManageService.selectFailureManageByFailureId(failureId);
        // 修改处理表
        if (failureManage.getDisposeId() != null)
            failureManage.setDispose(disposeService.selectDisposeById(failureManage.getDisposeId()));
        // 修改充电站
        if (failureManage.getChargerStationId() != null)
            failureManage.setChargingstation(chargingstationService.selectChargingstationByChargingstationId(failureManage.getChargerStationId()));
        return success(failureManage);
    }

    /**
     * 新增故障处理
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:failureDispose:add')")
    @Log(title = "故障处理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FailureManage failureManage) {
        return toAjax(failureManageService.insertFailureManage(failureManage));
    }

    /**
     * 修改故障处理
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:failureDispose:edit')")
    @Log(title = "故障处理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FailureManage failureManage) {
        // 修改处理表
        Dispose dispose = failureManage.getDispose();
        disposeService.updateDispose(dispose);
        return toAjax(failureManageService.updateFailureManage(failureManage));
    }

    /**
     * 删除故障处理
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:failureDispose:remove')")
    @Log(title = "故障处理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{failureIds}")
    public AjaxResult remove(@PathVariable Long[] failureIds) {
        return toAjax(failureManageService.deleteFailureManageByFailureIds(failureIds));
    }
}
